package cn.iocoder.yudao.module.gov.controller.admin.resourcecatalog;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.gov.controller.admin.resourcecatalog.vo.*;
import cn.iocoder.yudao.module.gov.dal.dataobject.resourcecatalog.ResourceCatalogDO;
import cn.iocoder.yudao.module.gov.service.resourcecatalog.ResourceCatalogService;

@Tag(name = "管理后台 - 政务资源菜单树")
@RestController
@RequestMapping("/gov/resource-catalog")
@Validated
public class ResourceCatalogController {

    @Resource
    private ResourceCatalogService resourceCatalogService;

    @PostMapping("/create")
    @Operation(summary = "创建政务资源菜单树")
    @PreAuthorize("@ss.hasPermission('gov:resource-catalog:create')")
    public CommonResult<Long> createResourceCatalog(@Valid @RequestBody ResourceCatalogSaveReqVO createReqVO) {
        return success(resourceCatalogService.createResourceCatalog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新政务资源菜单树")
    @PreAuthorize("@ss.hasPermission('gov:resource-catalog:update')")
    public CommonResult<Boolean> updateResourceCatalog(@Valid @RequestBody ResourceCatalogSaveReqVO updateReqVO) {
        resourceCatalogService.updateResourceCatalog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除政务资源菜单树")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('gov:resource-catalog:delete')")
    public CommonResult<Boolean> deleteResourceCatalog(@RequestParam("id") Long id) {
        resourceCatalogService.deleteResourceCatalog(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除政务资源菜单树")
                @PreAuthorize("@ss.hasPermission('gov:resource-catalog:delete')")
    public CommonResult<Boolean> deleteResourceCatalogList(@RequestParam("ids") List<Long> ids) {
        resourceCatalogService.deleteResourceCatalogListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得政务资源菜单树")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('gov:resource-catalog:query')")
    public CommonResult<ResourceCatalogRespVO> getResourceCatalog(@RequestParam("id") Long id) {
        ResourceCatalogDO resourceCatalog = resourceCatalogService.getResourceCatalog(id);
        return success(BeanUtils.toBean(resourceCatalog, ResourceCatalogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得政务资源菜单树分页")
    @PreAuthorize("@ss.hasPermission('gov:resource-catalog:query')")
    public CommonResult<PageResult<ResourceCatalogRespVO>> getResourceCatalogPage(@Valid ResourceCatalogPageReqVO pageReqVO) {
        PageResult<ResourceCatalogDO> pageResult = resourceCatalogService.getResourceCatalogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ResourceCatalogRespVO.class));
    }
    @GetMapping(value = {"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取政务资源信息列表")
    public CommonResult<List<ResourceCatalogRespVO>> getSimpleDeptList() {
        List<ResourceCatalogDO> list = resourceCatalogService.getResourceCatalogList();
        return success(BeanUtils.toBean(list, ResourceCatalogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出政务资源菜单树 Excel")
    @PreAuthorize("@ss.hasPermission('gov:resource-catalog:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportResourceCatalogExcel(@Valid ResourceCatalogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ResourceCatalogDO> list = resourceCatalogService.getResourceCatalogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "政务资源菜单树.xls", "数据", ResourceCatalogRespVO.class,
                        BeanUtils.toBean(list, ResourceCatalogRespVO.class));
    }

}